<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="称谓" prop="appellation" class="dislabel">
        <el-select v-model="pram.appellation" placeholder="称谓" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in nickList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="memberName" class="dislabel">
        <el-input v-model="pram.memberName" maxlength="100" placeholder="姓名"/>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday" class="dislabel">
        <el-date-picker v-model="pram.birthday" value-format="yyyy.MM" format="yyyy.MM" size="small" type="month" placement="bottom-end" style="width: 100%;" @change="onchangeBirthday" placeholder="出生日期"/>
      </el-form-item>
      <el-form-item label="政治面貌" prop="zzmm" class="dislabel">
        <el-select v-model="pram.zzmm" placeholder="政治面貌" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in zzmmList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone" class="dislabel">
        <el-input v-model="pram.phone" maxlength="50" placeholder="联系电话"/>
      </el-form-item>
      <el-form-item label="工作单位及职务" prop="unit" class="dislabel">
        <el-input v-model="pram.unit" type="textarea"  maxlength="250" :rows="4" placeholder="请输入工作单位及职务" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    editKey: {
      type: Number,
      required: true,
      default: -1,
    },
    allNickData:{
      type: Array
    },
    allZzmmData:{
      type: Array
    }
  },
  data() {
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请填写手机号'));
      } else if (!/^1[123456789]\d{9}$/.test(value)) {
        callback(new Error('手机号格式不正确!'));
      } else {
        callback();
      }
    };
    return {
      constants:this.$constants,
      pram: {
        appellation: null,
        memberName: null,
        birthday: null,
        zzmm:null,
        phone:null,
        unit:null,
        id: null,

        appellationText:'',
        zzmmText:'',
      },
      rules: {
        appellation: [{ required: true, message: '请选择称谓', trigger: ['blur', 'change'] }],
        memberName: [{ required: true, message: '请填姓名', trigger: ['blur', 'change'] }],
        birthday: [{ required: true, message: '请选择出生日期', trigger: ['blur', 'change'] }],
        zzmm: [{ required: true, message: '请选择政治面貌', trigger: ['blur', 'change'] }],
        phone: [{ required: true, message: '请填写正确的联系电话', validator: validatePhone, trigger: ['blur', 'change'] }],
        unit: [{ required: true, message: '请填写工作单位及职务', trigger: ['blur', 'change'] }],
      },

      nickList:[],
      zzmmList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideFamilyDialog')
    },

    onchangeBirthday (e) {
      this.pram.birthday = e;
    },

    initEditData() {
      this.nickList = this.allNickData
      this.zzmmList = this.allZzmmData

      if (this.isCreate !== 1) return //编辑
      const { id, appellation, memberName, birthday, zzmm, phone, unit} = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.id = id
      this.pram.appellation = appellation
      this.pram.memberName = memberName
      this.pram.birthday = birthday
      this.pram.zzmm = zzmm
      this.pram.phone = phone
      this.pram.unit = unit
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      const nickData = this.nickList
      const zzmmData = this.zzmmList
      nickData.forEach((item)=>{
        if(item.id === this.pram.appellation) this.pram.appellationText = item.name
      })
      zzmmData.forEach((item)=>{
        if(item.id === this.pram.zzmm) this.pram.zzmmText = item.name
      })

      this.$emit('assignFamilySaveAndHideFamilyDialog',this.pram)
    },
    handlerEdit() {
      const nickData = this.nickList
      const zzmmData = this.zzmmList
      nickData.forEach((item)=>{
        if(item.id === this.pram.appellation) this.pram.appellationText = item.name
      })
      zzmmData.forEach((item)=>{
        if(item.id === this.pram.zzmm) this.pram.zzmmText = item.name
      })

      this.$emit('assignFamilyEditAndHideFamilyDialog',this.pram,this.editKey)
    },
  }
}
</script>

<style scoped lang="scss">
  .dislabel{
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }
    ::v-deep.el-textarea__inner{
      font-size:12px;
    }

  }
</style>
